package com.dengyin2000.javaeye.ordering.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.dengyin2000.javaeye.ordering.entity.Dish;
import com.dengyin2000.javaeye.ordering.entity.DishOrder;
import com.dengyin2000.javaeye.ordering.entity.SnackShop;

@Name("appUtils")
@Scope(ScopeType.APPLICATION)
public class ApplicationUtils implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6104750807156821376L;
	
	@In("orderingDatabase")
	private Session session;

	public List<Dish> getMonDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getMonDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public List<Dish> getTuesDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getTuesDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public List<Dish> getWedDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getWedDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public List<Dish> getThuDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getThuDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public List<Dish> getFriDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getFriDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}

	public List<Dish> getSatDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getSatDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public List<Dish> getSunDishes(SnackShop shop){
		Set<Dish> dishesSet = shop.getSunDishes();
		List<Dish> list = new ArrayList<Dish>();
		list.addAll(dishesSet);
		Collections.sort(list, SnackShopAction.dishComparator);
		return list;
	}
	
	public SnackShop getOrderSnackShop(DishOrder order) {
		Set<Dish> dishes = order.getDishes();
		if (dishes == null || dishes.isEmpty())
			return null;
		
		Long snackShopId = dishes.iterator().next().getSnackShopId();
		return (SnackShop) session.load(SnackShop.class, snackShopId);
	}
}
